package com.hdkj.eduorder.controller;


import com.alipay.api.AlipayApiException;
import com.hdkj.commonutils.R;
import com.hdkj.eduorder.entity.vo.PayVo;
import com.hdkj.eduorder.service.PayLogService;
import com.hdkj.eduorder.utils.AlipayTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author 贾佩雨
 * @since 2022-05-18
 */
@RestController
@RequestMapping("/eduorder/paylog")
//@CrossOrigin
public class PayLogController {
    @Autowired
    private PayLogService payLogService;

    @Autowired
    private AlipayTemplate alipayTemplate;

//    //生成微信支付二维码接口
//    //参数是订单号
//    @GetMapping("createNative/{orderNo}")
//    public R createNative(@PathVariable String orderNo) {
//        //返回信息，包含二维码地址，还有其他需要的信息
//        Map map = payLogService.createNatvie(orderNo);
//        System.out.println("****返回二维码map集合:"+map);
//        return R.ok().data(map);
//    }

    @RequestMapping(value = "payorder/{ordersn}")
    public R payorder(@PathVariable String ordersn) throws AlipayApiException {
        PayVo payVo = payLogService.getorderpay(ordersn);
        String pay = alipayTemplate.pay(payVo);
        return R.ok().data("pay",pay);
    }

    //查询订单支付状态
    //参数：订单号，根据订单号查询 支付状态
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {
        Map<String,String> map = payLogService.queryPayStatus(orderNo);
        System.out.println("*****查询订单状态map集合:"+map);
        if(map == null) {
            return R.error().message("支付出错了");
        }
        //如果返回map里面不为空，通过map获取订单状态
        if(map.get("trade_state").equals("SUCCESS")) {//支付成功
            //添加记录到支付表，更新订单表订单状态
            payLogService.updateOrdersStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中");
    }
}

